package com.bxait.toymail.product.service.impl;

import com.bxait.com.utils.PageUtils;
import com.bxait.com.utils.Query;
import com.bxait.com.vo.product.BrandVo;
import com.bxait.com.vo.product.CateLogVo;
import com.bxait.toymail.product.entity.BrandEntity;
import com.bxait.toymail.product.entity.CategoryEntity;
import com.bxait.toymail.product.service.BrandService;
import com.bxait.toymail.product.service.CategoryService;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.bxait.toymail.product.dao.CategoryBrandRelationDao;
import com.bxait.toymail.product.entity.CategoryBrandRelationEntity;
import com.bxait.toymail.product.service.CategoryBrandRelationService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;


@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements CategoryBrandRelationService {

    @Resource
    @Qualifier("brandService")
    private BrandService brandService;

    @Resource
    @Qualifier("categoryService")
    private CategoryService categoryService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryBrandRelationEntity> page = this.page(
                new Query<CategoryBrandRelationEntity>().getPage(params),
                new QueryWrapper<CategoryBrandRelationEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void updateBatch(Long brandId,String name) {
        // 1.先查询在更新
        int count = this.count(new QueryWrapper<CategoryBrandRelationEntity>().eq("brand_id", brandId));
        if(count > 0){
            // 构建一个品牌分类的关联表实体类
            CategoryBrandRelationEntity
                    entity= CategoryBrandRelationEntity.build(brandId, name);
            // 更新品牌分类表的信息
            this.update(entity,new QueryWrapper<CategoryBrandRelationEntity>().eq("brand_id",brandId));
        }
    }

    /**
     * @param catId
     * @==============History===============<br/>
     * @Description // TODO  根据分类id,获取属性分类下的品牌信息
     * @Date 2023/3/20 20:01
     * @Author <a href="qq:2277411582@qq.com">chen</a>
     * @return java.util.List<com.bxait.com.vo.product.BrandVo>
     */
    @Override
    public List<BrandVo> getBrandsByCatId(Long catId) {

        List<CategoryBrandRelationEntity> categoryBrandRelationEntities
                   = this.list(new QueryWrapper<CategoryBrandRelationEntity>().eq("catelog_id",catId));

        List<Long> brandIds =
                  categoryBrandRelationEntities.stream().map(item -> item.getBrandId()).collect(Collectors.toList());

        List<BrandEntity> brandEntities =
                brandService.getBaseMapper().selectBatchIds(brandIds);

        List<BrandVo> list = new ArrayList<>();

        brandEntities.stream().forEach(item -> {
            BrandVo brandVo = new BrandVo();
            brandVo.setBrandId(item.getBrandId());
            brandVo.setBrandName(item.getName());
            list.add(brandVo);
        });

        return list;
    }

    /**
     * @param brandId
     * @==============History===============<br/>
     * @Description // TODO  获取品牌关联的分类
     * @Date 2023/3/20 20:14
     * @Author <a href="qq:2277411582@qq.com">chen</a>
     * @return java.util.List<com.bxait.com.vo.product.CateLogVo>
     */
    @Override
    public List<CateLogVo> getCateLogByBrandId(Long brandId) {
        List<CategoryBrandRelationEntity> categoryBrandRelationEntities
                = this.list(new QueryWrapper<CategoryBrandRelationEntity>().eq("brand_id",brandId));

        BrandEntity brandEntity = brandService.getById(brandId);

        List<Long> cateLogIds
                = categoryBrandRelationEntities.stream().map(item -> item.getCatelogId()).collect(Collectors.toList());

        List<CategoryEntity> categoryEntities
                = categoryService.getBaseMapper().selectBatchIds(cateLogIds);

        List<CateLogVo> collect = categoryEntities.stream().map(item -> {
            CateLogVo cateLogVo = new CateLogVo();
            cateLogVo.setCateLogId(item.getCatId());
            cateLogVo.setCateLogName(item.getName());
            cateLogVo.setBrandName(brandEntity.getName());
            return cateLogVo;
        }).collect(Collectors.toList());
        return collect;
    }

    /**
     * @param categoryBrandRelation
     * @==============History===============<br/>
     * @Description // TODO
     * @Date 2023/3/20 20:21
     * @Author <a href="qq:2277411582@qq.com">chen</a>
     * @return void
     */
    @Transactional
    @Override
    public void saveDetail(CategoryBrandRelationEntity categoryBrandRelation) {
        // 获取品牌的详情名称,与分类的名称
        Long brandId = categoryBrandRelation.getBrandId();
        Long cateLogId = categoryBrandRelation.getCatelogId();

        BrandEntity brandEntity = brandService.getById(brandId);
        if(brandEntity != null){
            // categoryBrandRelation.setBrandId(brandEntity.getBrandId());
            categoryBrandRelation.setBrandName(brandEntity.getName());
        }

        CategoryEntity categoryEntity = categoryService.getById(cateLogId);
        if(categoryEntity != null){
            categoryBrandRelation.setCatelogName(categoryEntity.getName());
        }

        this.save(categoryBrandRelation);

    }

    /**
     * @param catId
     * @param name
     * @==============History===============<br/>
     * @Description // TODO  修改关联表中的分类信息
     * @Date 2023/4/8 2023/4/8 
     * @Author < a href="2277411582@qq.com">chen</ a>
     * @return [java.lang.Long, java.lang.String]
     */
    @Override
    public void updateCategory(Long catId, String name) {
        this.baseMapper.updateCategory(catId,name);
    }


}